<!--订单录入-->
<include file="../header" />
<body>
	<div class="wraper">
		<div class="load">
			<img class="load-bee" src="/assets/base/repair/images/load-bee.png"/>
		</div>
	    <div class="top">
	        <p>
	        	<a href="/Repairnew/Repairnew/index/repairId/{BEESCRM:$repairId}" class="iconfont icon-zuo fl-left"></a>
	        	<span class="fl-left">订单录入</span>
	        	<a href="/Repairnew/Repairnew/index/repairId/{BEESCRM:$repairId}" class="iconfont icon-shouye text-right fl-right"></a>
	        </p>
	    </div>
	   <div class="upload-img clearfix">
		   <div class="imgbox fl-left"><img src="{BEESCRM:$data['img']['url']}" id="picture" style="width:100px;height:100px;"/></div>
		   <div class="add-img fl-left" >
			   <input class="file-input" type="file" accept="image/&" name="upload-file" id="fileupload_input"/>
			   <span class="iconfont icon-tianjia tianjia"></span>
		   </div>
    	</div>
    <div>
        <p class="form-box">
            <textarea class="textarea" placeholder="请用简短的话描述要报修的情况" id="content">{BEESCRM:$data.content}</textarea>
        </p>
        <p class="form-box">
			<select class="select" id="order_type">
				<option value="0">订单类型</option>
				<option value="5">补录订单</option>
				<option value="4">巡检订单</option>
			</select>
			<span class="triangle"></span>
		</p>

		<!-- <p class="form-box">
			<input  style="height:100px;" type="radio">dasdf<input  style="height:100px;" type="radio">dasdf
			
		</p> -->
		
		<p class="form-box">
			<select class="select" id="community_type">
				<option value="0">请选择园区</option>
				<option value="1">生活区</option>
				<option value="2">教学区</option>
			</select>
			<span class="triangle"></span>
		</p>
        <p class="form-box user_msg" style="display:none">
            <select class="select" id="project_id">
            	<option value="0">请选择报修一级项目</option>
            	<!-- <volist name="projectList" id="vo">
            		<option value="{BEESCRM:$vo.project_id}" <if condition="$data['project_id'] eq $vo['project_id']">selected="selected"</if>>
            			{BEESCRM:$vo['project_name']}
            		</option>
            	</volist> -->
            </select>
            <span class="triangle"></span>
        </p>
        <p class="form-box" id="child_project" style="display: none;">
            <select class="select" id="project_child_id" >
            </select>
            <span class="triangle"></span>
        </p>

	    <p class="form-box">
	        <select class="select" id="community">
	            <option value="0">请选择园区</option>
	            <volist name="community" id="vo">
	                <option value="{BEESCRM:$vo.community_id}"
	                <if condition="$addressInfo['community_id'] eq $vo['community_id']"> selected="selected"</if>
	                >{BEESCRM:$vo.community_name}</option>
	            </volist>
	        </select>
	        <span class="triangle"></span>
	    </p>
	    <p class="form-box">
	        <select class="select" id="building">
	                <option value="0">请选择楼幢</option>
	        </select>
	        <span class="triangle"></span>
	    </p>

	    <p class="form-box">
	        <select class="select" id="floor">
	               <option value="0">请选择楼层</option>
	        </select>
	        <span class="triangle"></span>
	    </p>

	    <p class="form-box">
	        <select class="select" id="room">
	                <option value="0">请选择寝室</option>
	        </select>
	        <span class="triangle"></span>
	    </p>
	    <p class="form-box user_msg" style="display:hide;">
	        <select class="select" id="users">
	                <option value="0">请选择用户</option>
	                <option value="-1">手动填写</option>
	        </select>
	        <p class="form-box user_message"  style="display:none;">
	        	<input class="input" value="" id="real_name" placeholder="姓名"/>
	        </p>
	        <p class="form-box user_message"  style="display:none;">
	        	<input class="input" value="" id="number" placeholder="学号"/>
	        </p>
	        <p class="form-box user_message"  style="display:none;">
	        	<input class="input" value="" id="mobile" placeholder="手机号"/>
	        </p>
	   
	    </p>
	    <p class="form-box">
	        <input class="input" value="" id="addressDetail" placeholder="其他地址" />
	    </p>
    </div>
    <div class="marginb-50"></div>
    <ul class="btn-box">
    	<input type="hidden" id="address_id" value="{BEESCRM:$data['address_id']}">
    	<input type="hidden" id="order_id" value="{BEESCRM:$data['order_id']}">
    	<input type="hidden" id="cost" value="0">
        <a href="javascript:void(0)" class="btn half addorder  blue-btn-box" >提交</a>
        <a href="javascript:history.back(-1)" class="btn half js_cancel  blue-btn-box" >取消</a>
    </ul>
</div>

	<input type="hidden" name="imageNeed" id="imageNeed" value="{BEESCRM:$repairInfo['image_condition']}">

<include file="../commonJS" />
<script type="text/javascript" src="/assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.fileupload.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.iframe-transport.js"></script>
<script type="application/javascript" src="/assets/repairnew/js/apartment.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		//图片上传
		$("#fileupload_input").fileupload({
			url:"/Base/Common/upload",//文件上传地址，当然也可以直接写在input的data-url属性内
			beforeSend:function(){
				$('.load').show();
			},
			done:function(e,result){
				//done方法就是上传完毕的回调函数，其他回调函数可以自行查看api
				//注意result要和jquery的ajax的data参数区分，这个对象包含了整个请求信息
				//返回的数据在result.result中，假设我们服务器返回了一个json对象
				var data=$.parseJSON(result.result);
				if(data.status=='S'){
					$('#picture').attr('src',data.url);
					setTimeout($('.load').hide(),3000);
				}else{
					$.alert('error',data.message);
					setTimeout($('.load').hide(),3000);
					return false;
				}
			}
		});

		$('#order_type').change(function(){
			var orderType = $(this).val();
			if(orderType == 5){
				$('.user_msg').show();
			}else{
				$('.user_msg').hide();
			}
		})

		//根据区域获得报修分类
		$('#community_type').change(function(){
			var area = $(this).val();
			$.ajax({
				url:"/Repairnew/RepairnewReceive/getFirstProject/repairId/{BEESCRM:$repairId}",
				type:"post",
				data:{'area':area},
				dataType:'json',
				success:function(res){
					var str = '<option value="0">请选择报修一级项目</option>';
					if(res.list && res.list.length>0){
						var arr = res.list;
						for(var i = 0 ; i<arr.length; i++){
							str+='<option value="'+arr[i].project_id+'">'+arr[i].project_name+'</option>';
						}
					}
					$('#project_id').html(str);

				}
			})
		})

		//获取二级项目列表
		$("#project_id").change(function () {
			var project_id = $(this).val();
			if(project_id == 0){	//选中请选择，不做任何操作
				$('#project_child_id').html('<option value="0">请选择报修二级项目</option>');
				$('#child_project').hide();
				return false;
			}

			var _html = '<option value="0">请选择报修二级项目</option>';
			$.ajax({
				url : '/Repairnew/RepairnewUserOrder/ajaxChildProjectList/repairId/{BEESCRM:$repairId}',
				type : 'POST',
				data : {'project_id' : project_id},
				dataType : 'JSON',
				success : function (data) {
					if(data.status == 'S'){
						for(var i=0; i< data.data.length; i++){
							_html += '<option value="' + data.data[i].project_id + '">' + data.data[i].project_name +'</option>';
						}
						$('#project_child_id').html(_html);
						$('#child_project').show();
					}else{
						$('#child_project').hide();
					}
				},
				error : function () {
					$.alert('error','网络错误，请重试');
				}
			});

		});

		//点击搜索园区、楼幢等联动
        ApartmentObj.init($('#community'),$('#building'),$('#floor'),$('#room'));
        $('#community').on('change',function () {
            ApartmentObj.communityChange();
        });
        $('#building').on('change',function () {
            ApartmentObj.buildingChange();
        });
        $('#floor').on('change',function () {
            ApartmentObj.floorChange();
        });
        $('#room').on('change',function () {
            ApartmentObj.roomChange();

            //获得房间下的用户
            $.ajax({
            	url:"/Repairnew/RepairnewReceive/ajaxGetUser/repairId/{BEESCRM:$repairId}",
            	type:"post",
            	data:{'room_id':$(this).val()},
            	dataType:"json",
            	success:function(res){
            		var optionStr = '<option value="0">请选择用户</option>';
            		if(res.data.length>0){
            			var dataArr = res.data;
            			for(var i=0;i<dataArr.length;i++){
            				optionStr+='<option value="'+dataArr[i].user_id+'" data-number="'+dataArr[i].number+'" data-mobile="'+dataArr[i].mobile+'">'+dataArr[i].real_name+'</option>';
            			}
            		}
            		optionStr+='<option value="-1">手动填写</option>';
            		$('#users').html(optionStr);
            	}
            })
        });

        $('#users').change(function(){
        	var userId = $(this).val();
        	if(userId == -1){
        		$('.user_message').show();
        	}else{
        		$('.user_message').hide();
        	}
        })

		
		//提交 创建订单
		$(document).on('click','.addorder',function () {
			var img_url = $('#picture').attr('src');
			var content = $('#content').val();
			var order_type =$('#order_type').val();
			var community_type = $('#community_type').val();

			var project_id = $('#project_id').val();
			var project_child_id = $('#project_child_id').val();
			var community_id = $('#community').val();

			var community_name = $('#community').find('option:selected').text();
			var building_id = $('#building').val();
			var building_name = $('#building').find('option:selected').text();
			var room_id = $('#room').val();
			var room_name = $('#room').find('option:selected').text();
			var real_name = $('#real_name').val();
			var mobile = $('#mobile').val();
			var number = $('#number').val();
			var patten = /^1[3|4|5|7|8]\d{9}$/;
			var user_id = $('#users').val();
			var detail_address = $('#addressDetail').val();
			if(!img_url){
				$.alert('error','请上传报修图片');
				return false;
			}

			if(!content){
				$.alert('error','请填写报修内容');
				return false;
			}

			if(order_type == 0){
				$.alert('error','请选择订单类型');
				return false;
			}

			if(community_type == 0){
				$.alert('error','请选择园区');
				return false;
			}

			
			if(community_id == 0 || building_id == 0 || room_id == 0){
				$.alert('error','请选择报修地址');
				return false;
			}
			

			var data = {
				'url' :img_url,
				'content' : content,
				'order_type':order_type,
				'community_type' : community_type,
				
				'community_id'		: community_id,
				'building_id'		: building_id,
				'room_id'			: room_id,
				'community_name' : community_name,
				'building_name' : building_name,
				'room_name' : room_name,
				'detail_address':detail_address
			};
			if(data.order_type==5){
				if(user_id == -1){//手动填写
					if(!real_name){
						$.alert('error','请填写报修人姓名');return false;
					}
					if(!number){
						$.alert('error','请填写学号');return false;
					}

					if(!patten.test(mobile)){
						$.alert('error','请正确填写手机号');return false;
					}

					data.real_name = real_name;
					data.mobile = mobile;

					

				}else if(user_id>0){//选择
					data.user_id = user_id;
					data.real_name = $('#users').find('option:selected').text();
					data.mobile = $('#users').find('option:selected').data('mobile');
					data.number = $('#users').find('option:selected').data('number');
				}
				if(!project_id  || project_id == 0){
					$.alert('error','请选择报修项目');
					return false;
				}

				data.project_id = project_id;
				data.project_child_id = project_child_id;
			}
			$.ajax({
				url : '/Repairnew/RepairnewReceive/ajaxCreateOrder/repairId/{BEESCRM:$repairId}',
				type : 'POST',
				data : data,
				dataType : 'JSON',
				beforeSend : function () {

				},
				success : function (res) {

					if(res.status == 'S'){
						$.alert('success','提交成功');
						var url = '/Repairnew/Repairnew/index/repairId/{BEESCRM:$repairId}/type/{BEESCRM:$type}';
						setTimeout("location.href = '" + url+ "'",2000);
					}else{
						$.alert('error',res.message);
					}
				},
				error : function () {
					$.alert('error','网络错误，请重试');
				}
			})
		});

	});
</script>
</body>
</html>
